package com.abc.easyclaim;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.abc.bean.Record;
import com.abc.db.DBManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;

public class RecordCommandReciver extends BroadcastReceiver {

	MediaRecorder mRecorder;          //录音引用
	int status;//状态值
	private String soundFileName;            //要录音的默认文件路径
	private int defaultNum;               //要录音的默认文件序号
	private int record_time;       //记录文件录制时间长度
	private Runnable runnable;                 //控制进度条线程
    private Thread td;                    //线程
    private boolean stopThread = false;
	private File soundFiles;         //录制的音频文件
	private List<Record> records=new ArrayList<Record>();         //录音集合
    private DBManager dbManager;           //数据库操作管理器  
	
	@Override
	public void onReceive(final Context context, Intent intent) {
		// TODO Auto-generated method stub
		//从数据库中读取录音信息
		dbManager=new DBManager(context);
		records=dbManager.queryRecords();
		
		//进度条线程
		runnable=new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					while(status==Constant.STATUS_RECORD && !stopThread){
						Thread.sleep(1000);
					    record_time++;              //录音时间增加
					    if (status==Constant.STATUS_STOPRECORD) {
							record_time=0;
						}
					    //System.out.println("record_time="+record_time);
						//System.out.println("status="+status);
						//System.out.println("线程ID:"+Thread.currentThread().getId());  
			            //System.out.println("线程name:"+Thread.currentThread().getName());
			            //更新界面
						updateUI(context);
				    }
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
		};
		
		switch(intent.getIntExtra("cmd", -1))
		{
		 //根据收到命令的不同做不同的工作
		    //收到录音命令
		case Constant.COMMAND_RECORD:
			if(!Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
			{
				return;
			}
			try {
				defaultNum=intent.getIntExtra("defaultNum", -1);
				if (defaultNum!=-1) {               //新一轮的录音
					soundFileName=intent.getStringExtra("soundFileName");
					soundFiles=new File(soundFileName);   //已有文件则序号加1，否则从0开始
					record_time=0;                   //录音时间归0
				}			
				if (mRecorder!=null) {
					mRecorder.release();
					mRecorder=null;
				}
				mRecorder=new MediaRecorder();
				//设置录音来自麦克风
				mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
				//设置录音的输出格式
				mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
				//设置录音的编码格式
				mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
				//设置录音的输出路径
				mRecorder.setOutputFile(soundFiles.getAbsolutePath());
				mRecorder.prepare();
				mRecorder.start();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			//更新状态值到录音中状态
		    status=Constant.STATUS_RECORD;
		    stopThread=false;
			td=new Thread(runnable); 
			td.start();              //启动线程
			break;
			//收到暂停录音命令
		case Constant.COMMAND_PAUSERECORD:
			//更新状态值到暂停录音状态
			//停止录音
			mRecorder.stop();
			//释放资源
			mRecorder.release();
			mRecorder=null;
			status=Constant.STATUS_PAUSERECORD;
			stopThread=true;
			td=new Thread(runnable); 
			td.start();              //启动线程
			break;
			//收到停止录音命令
		case Constant.COMMAND_STOPRECORD:
			//更新状态值到停止录音状态
			//停止录音
			if (mRecorder!=null) {
				mRecorder.stop();
				//释放资源
				mRecorder.release();
				mRecorder=null;
			}
			status=Constant.STATUS_STOPRECORD;
			stopThread=true;
			td=new Thread(runnable); 
			td.start();              //启动线程
			break;
		}
		
		//更新界面
		updateUI(context);
	}
	
	public void updateUI(Context context){
		//创建Action为Constant.UPDATE_STATUS（更新界面）的Intent
		Intent intent =new Intent(Constant.RECORD_UPDATE);
		//将更新后的新状态值放到intent中
		intent.putExtra("status", status);
		intent.putExtra("defaultNum", defaultNum);
		intent.putExtra("record_time", record_time);
		//广播Intent
		context.sendBroadcast(intent);
	}
}
